import filterData from '@/common/filterData';
import formatter from '@/common/formatter';

const base = { title: '智家业务在途占比' }
const dataset = {
  source: [
    ['FTTR', 300],
    ['组网', 580],
    ['看家', 1048],
    ['门锁', 735],
    ['平安乡村', 484]
  ]
};

function getChartData([dataPool], condition) {
  const bizs = [
    { biz: 'FTTR', displayName: 'FTTR' },
    { biz: 'zuwang', displayName: '组网' },
    { biz: 'kanjia', displayName: '看家' },
    { biz: 'mensuo', displayName: '门锁' },
    { biz: 'pinganxiangcun', displayName: '平安乡村' }
  ];
  const hasCondition = condition && condition.length && (condition[0].length || condition[1].length);
  const _condition = hasCondition ? [...condition[0], ...condition[1]] : []
  const collectionData = dataPool['xinyewu_zt'];

  const data = bizs.map(({ biz, displayName }) => {
    let xyw_zt = collectionData[biz];
    const latestDate = Math.max(...xyw_zt.map(i => i.date));
    xyw_zt = xyw_zt.filter(i => i.date === latestDate);

    const bizCount = filterData.count(xyw_zt, _condition);
    return [displayName, bizCount]
  })

  this.dataset.source = data;
  return data;
}


// // ======更新内容↑========

export default {
  title: {
    text: base.title,
    left: 'center',
  },
  legend: {
    left: 'center',
    bottom: '0%',
    type: 'scroll'
  },
  grid: {
    width: '100%',
    height: '100%',
    left: '0%',
    right: '0%',
    top: '0%',
    bottom: '0%',
    containLabel: true
  },
  dataset,
  tooltip: {
    showContent: true,
    formatter: (params) => `<div>${params.marker} ${params.data[0]} </br> ${params.data[1]}（${params.percent}%）</div>`
  },
  series: [
    {
      type: 'pie',
      radius: ['30%', '60%'],
      labelLine: { smooth: true }
    }
  ],
  getChartData
}